import business.*
import enums.Direction
import javafx.scene.input.KeyCode
import javafx.scene.input.KeyEvent
import model.*
import model.Map
import org.itheima.kotlin.game.core.Window

class GameWindow :Window("坦克大战","img/logo.jpg",Config.gameWidth,Config.gameHeight){
    private var map=Map("map/1.map")
    private lateinit var tank:Tank
    private var isGameOver=false
    override fun onCreate() {
        tank= Tank(9,9)
        map.list.add(tank)
    }

    override fun onDisplay() {
        //println("list:${map.list.size}")
        map.list.forEach {
            it.draw()
        }
    }

    override fun onKeyPressed(event: KeyEvent) {
        when(event.code){
            KeyCode.W -> tank.move(Direction.UP)
            KeyCode.S -> tank.move(Direction.DOWN)
            KeyCode.A -> tank.move(Direction.LEFT)
            KeyCode.D -> tank.move(Direction.RIGHT)
            KeyCode.ENTER ->{
                tank.fire()
                map.list.add(tank.bullet)
            }
        }
    }

    override fun onRefresh() {
        map.list.filter { it is Destroyable }.forEach {
            it as Destroyable
            if(it.isDestroy()){
                map.list.remove(it)
            }
        }
        if(isGameOver){
            return
        }

        if(map.list.filter { it is Enemy }.size<map.enemyActiveCount){
            map.enemyLocation.forEach {
                map.list.add(Enemy(it.first,it.second))
                map.enemyCount--
            }
        }
        //碰撞检测
        map.list.filter {it is Moveable}.forEach  moveTag@{
            move ->
                var badDirection:Direction?=null
                var badBlock:Blockable?= null
                if (move is Tank){
                    move as Tank
                    move.fire()
                    if(move.bullet!=null){
                        map.list.add(move.bullet)
                    }
                }
                if (move is Enemy){
                    move as Enemy
                    move.fire()
                    if(move.bullet!=null){
                        map.list.add(move.bullet)
                    }
                }
                move as Moveable
                map.list.filter { it is Blockable&&it!=move }.forEach blockTag@{ block ->
                    block as Blockable
                    var direction: Direction? = move.willCollision(block)
                    direction?.let {
                        //println(direction)
                        badDirection=direction
                        badBlock=block
                        return@blockTag
                    }
                }
                move.notifyCollision(badDirection,badBlock)
        }

        map.list.filter { it is AutoMove }.forEach {
            it as AutoMove
            it.autoMove()
        }

        map.list.filter { it is Attackable }.forEach attack@{
            attack ->attack as Attackable
                map.list.filter { it is Sufferable&&it!=attack }.forEach suffer@{
                    suffer ->suffer as Sufferable
                    val collision=attack.isCollision(attack.x,attack.y,attack.width,attack.height,suffer.x,suffer.y,suffer.width,suffer.height)
                    if(collision){//发生碰撞
                        attack.notifyAttack(suffer)
                        val sufferViews=suffer.notifySuffer(attack)
                        if(sufferViews!=null){
                            map.list.addAll(sufferViews)
                        }
                        return@suffer
                    }
                }
        }

        if(map.list.filter { it is Tank }.isEmpty()||map.list.filter { it is Camp }.isEmpty()||map.enemyCount<0){
            isGameOver=true
            println(isGameOver)
        }

    }
}